package com.spider.admin.store.primary.mapper;

import com.spider.admin.store.primary.model.AccountInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AccountMapper {
    /**
     *
     * @param accountInfo
     * @return
     */
    @Insert("INSERT INTO t_account_info(pid,account_name,password,real_name,account_desc,tenant_id,tenant_name,archive_status,account_type,create_time,update_time)" +
            " values(#{pid},#{accountName},#{password},#{realName},#{accountDesc},#{tenantId},#{tenantName},#{archiveStatus},#{accountType},NOW(),NOW()) ")
    int saveAccount(AccountInfo accountInfo);

    /**
     *
     * @param accountName
     * @return
     */
    @Select("Select * from t_account_info where account_name=#{accountName}")
    @Results(value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "account_name",property = "accountName"),
            @Result(column = "real_name",property = "realName"),
            @Result(column = "account_desc",property = "accountDesc"),
            @Result(column = "tenant_id",property = "tenantId"),
            @Result(column = "tenant_name",property = "tenantName"),
            @Result(column = "account_type",property = "accountType"),
            @Result(column = "archive_status",property = "archiveStatus")
    })
    AccountInfo getAccountByName(@Param("accountName") String accountName);

    /**
     * 分页查询账户清单列表
     * @param startRow
     * @param pageSize
     * @return
     */
    @Select({"<script>",
            "Select pid,account_name,real_name,account_desc,tenant_id,tenant_name,account_type,archive_status from t_account_info WHERE 1=1 ",
            "<if test='accountName != null'> AND account_name = #{accountName}</if>",
            "<if test='realName != null'> AND real_name = #{realName}</if>",
            "<if test='accountType != null'> AND account_type = #{accountType}</if>",
            "<if test='accountStatus != null'> AND archive_status = #{accountStatus}</if>",
            " limit #{pageSize} offset #{startRow}",
            "</script>"})
    @Results(value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "account_name",property = "accountName"),
            @Result(column = "real_name",property = "realName"),
            @Result(column = "account_desc",property = "accountDesc"),
            @Result(column = "tenant_id",property = "tenantId"),
            @Result(column = "tenant_name",property = "tenantName"),
            @Result(column = "account_type",property = "accountType"),
            @Result(column = "archive_status",property = "archiveStatus")
    })
    List<AccountInfo> queryAccounts(int startRow,int pageSize,String accountName,
                                    String realName,
                                    Integer accountType,
                                    String accountStatus);

    /**
     *
     * @return
     */
    @Select({"<script>",
            "Select count(0) from t_account_info WHERE 1=1 ",
            "<if test='accountName != null'> AND account_name = #{accountName}</if>",
            "<if test='realName != null'> AND real_name = #{realName}</if>",
            "<if test='accountType != null'> AND account_type = #{accountType}</if>",
            "<if test='accountStatus != null'> AND archive_status = #{accountStatus}</if>",
            "<if test='pid != null'> AND pid = #{pid}</if>",
            "</script>"})
    Long getAccountRecordNum(String accountName,
                             String realName,
                             Integer accountType,
                             String accountStatus,String pid);

    /**
     *
     * @param tenantId
     * @return
     */
    @Select("Select count(0) ct from t_account_info where tenant_id=#{tenantId}")
    long getTenantCountById(String tenantId);

    /**
     *
     * @param accountName
     * @param tenantName
     * @return
     */
    @Select("Select count(0) ct from t_account_info where account_name<>#{accountName} and tenant_name=#{tenantName}")
    long getTenantCountByName(String accountName,String tenantName);

    /**
     *
     * @param tenantId
     * @return
     */
    @Select("Select count(0) ct from t_account_info where tenant_id=#{tenantId} and archive_status='active'")
    long getActiveTenantCountById(String tenantId);

    /**
     *
     * @param accountInfo
     * @param accountName
     * @return
     */
    @Update({
            "<script>",
            "UPDATE t_account_info",
            "<set>",
            "update_time=NOW()",
            "<if test='accountInfo.realName != null'>,real_name = #{accountInfo.realName}</if>",
            "<if test='accountInfo.accountDesc != null'>,account_desc = #{accountInfo.accountDesc}</if>",
            "<if test='accountInfo.tenantName != null'>,tenant_name = #{accountInfo.tenantName}</if>",
            "<if test='accountInfo.archiveStatus != null'>,archive_status = #{accountInfo.archiveStatus}</if>",
            "<if test='accountInfo.accountType != null'>,account_type = #{accountInfo.accountType}</if>",
            "</set>",
            "WHERE account_name=#{accountName}",
            "</script>"
    })
    int updateAccount(AccountInfo accountInfo,String accountName);

    /**
     *
     * @param accountInfo
     * @param accountName
     * @return
     */
    @Update({
            "<script>",
            "UPDATE t_account_info",
            "<set>",
            "update_time=NOW()",
            "<if test='accountInfo.realName != null'>,real_name = #{accountInfo.realName}</if>",
            "<if test='accountInfo.accountDesc != null'>,account_desc = #{accountInfo.accountDesc}</if>",
            "<if test='accountInfo.tenantName != null'>,tenant_name = #{accountInfo.tenantName}</if>",
            "<if test='accountInfo.password != null'>,password = #{accountInfo.password}</if>",
            "</set>",
            "WHERE account_name=#{accountName}",
            "</script>"
    })
    int updateAccountByOwner(AccountInfo accountInfo,String accountName);


    /**
     *
     * @param md5Pwd
     * @param accountName
     * @return
     */
    @Update("UPDATE t_account_info SET password = #{md5Pwd} WHERE account_name=#{accountName}")
    int updateAccountPwd(String md5Pwd,String accountName);
}

